package com.zhangtai.modules.service.impl.salary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.UserBankCardDao;
import com.zhangtai.modules.dto.UserBankCardDto;
import com.zhangtai.modules.entity.salary.UserBankCardEntity;
import com.zhangtai.modules.service.salary.UserBankCardService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class UserBankCardServiceImpl extends ServiceImpl<UserBankCardDao, UserBankCardEntity> implements UserBankCardService {

    @Autowired
    private UserBankCardDao userBankCardDao;
    @Override
    public PageResult queryPage(@RequestParam(value = "oid", required = false) String oid,
                                @RequestParam(value = "isPaycard", required = false) String isPaycard,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<UserBankCardEntity> page = this.page(
                new Page<UserBankCardEntity>(pageNum,pageSize),
                new QueryWrapper<UserBankCardEntity>()
                        .eq(!StringUtils.isEmpty(oid),"oid", oid)
                        .eq(!StringUtils.isEmpty(isPaycard),"is_paycard", isPaycard)
                        .eq("is_delete",0)
                        .orderByDesc("is_paycard")
                        .orderByDesc("create_time")
        );

        return new PageResult(page);
    }
    @Override
    public  List<UserBankCardDto>  getUserBankCardPage(@RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                                         @RequestParam(value = "name", required = false) String name,
                                                         @RequestParam(value = "code", required = false) String code,
                                                         @RequestParam(value = "orgname", required = false) String orgname,
                                                         @RequestParam(value = "unitname", required = false) String unitname,
                                                         @RequestParam(value = "jobname", required = false) String jobname,
                                                         @RequestParam(value = "status") Integer status,
                                                      Page<UserBankCardDto> page) {


        List<UserBankCardDto> list = userBankCardDao.getUserBankCardPage(
                orgIds,
                StringUtils.isEmpty(name)?null:name,
                StringUtils.isEmpty(code)?null:code,
                StringUtils.isEmpty(orgname)?null:orgname,
                StringUtils.isEmpty(unitname)?null:unitname,
                StringUtils.isEmpty(jobname)?null:jobname,
                status,page);
        page.setRecords(list);
        return list;
    }


    @Override
    public UserBankCardEntity getByIdE(Long id) {
            UserBankCardEntity one = this.getOne(new QueryWrapper<UserBankCardEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(UserBankCardEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(UserBankCardEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            UserBankCardEntity entity = new UserBankCardEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}